package com.sunop.panticlass.task;


import com.sunop.panticlass.mapper.ClassNoiceMapper;
import com.sunop.panticlass.model.ClassPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ClassroomMessageConsumer {

    @Autowired
    private ClassNoiceMapper classNoiceMapper;

    @RabbitListener(
            queues = "#{@dynamicQueueResolver.resolveQueues()}",
            containerFactory = "dynamicListenerContainerFactory",
            ackMode = "MANUAL" // 避免自动ACK导致消息丢失
    )
    public void processClassroomMessage(ClassPO message) {
        try {
            log.info("收到班级 {} 的消息: {} (教师: {})",
                message.getClassId(),
                message.getContent(),
                message.getTeacherName());
            // 将消息插入数据库
            classNoiceMapper.insert(message);
        } catch (Exception e) {
            log.error("处理班级消息失败: {}", e.getMessage());
        }
    }
}